package com.xian.service;

import com.xian.dao.UserMapper;
import com.xian.pojo.Role;
import com.xian.pojo.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    //密码加密
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = userMapper.findByUsername(username);
        // 处理自己的用户对象，封装成UserDetail,同时获得用户的角色
        //User user = new User(userInfo.getUsername(),userInfo.getPassword(),getAuthority(userInfo.getRoles()));
        User user = new User(userInfo.getUsername(),
                userInfo.getPassword(),
                userInfo.getStatus()==0?false:true,
                true,
                true,
                true,
                getAuthority(userInfo.getRoles()));
        return user;
    }
    // 将用户角色封装成SimpleGrantedAuthority类型的List
    public List<SimpleGrantedAuthority> getAuthority(List<Role> roles){
        List<SimpleGrantedAuthority> list = new ArrayList<SimpleGrantedAuthority>();
        // 判断用户是否有角色，没有则不能登录，不判断则是能登录，不能执行其他操作
        if (roles != null && roles.size() != 0) {
            for (Role role : roles) {
                System.out.println(role);
                list.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleName()));
            }
            return list;
        }
        return null;
    }
     // 查询所有用户
    public List<UserInfo> findAll() {
        List<UserInfo> all = userMapper.findAll();
        for (UserInfo userInfo : all) {
            System.out.println(userInfo.getId());
        }
        return all;
    }
    // 添加用户
    public int addUser(UserInfo userInfo) {
        //对密码加密处理
        String encode = bCryptPasswordEncoder.encode(userInfo.getPassword());
        userInfo.setPassword(encode);
        return userMapper.addUser(userInfo);
    }
    // 根据ID查询用户
    public UserInfo findById(String id) {
        return userMapper.findById(id);
    }

    // 查询指定用户没有的角色
    public List<Role> findNoRole(String userId) {
        return userMapper.findNoRole(userId);
    }
    //  给指定的用户添加 角色
    public void addRole(String userId, String[] roleIds) {
        if (roleIds != null && roleIds.length != 0){
            for (String roleId : roleIds) {
                userMapper.addRole(userId,roleId);
            }
        }

    }
}
